$(function () {
    var thisPage = null, user = null, html = [], addressId = '';
    var addAddressPage = function () {
        this.init()
    };

    addAddressPage.prototype = {
        init: function () {
            this.initData();
            this.bindEvent();
        },
        initData: function () {
            user = getLocalStorage(STORAGE_MANAGER.user);
            if (!user || !user.id) {
                redirect("login.html");
                return false;
            }

            addressId = getUrlParam('id');
            if (addressId) {
                $.ajax({
                    type: "GET",
                    url: CONFIG_MANAGER.RequstUrl + "v1/member/address/" + user.id + "/" + addressId,
                    async: true
                }).done(function (data) {
                    $("#address").val(data.address);
                    $("#consignee").val(data.consignee);
                    $("#mobile").val(data.mobile);
                    if (data.default_flag) {
                        $("#default_flag").prop("checked", true);
                    }
                });
            }

            this.loadRegion();
        },
        //区域
        loadRegion: function () {

            $.ajax({
                type: "GET",
                url: CONFIG_MANAGER.RequstUrl + "v1/basic/region/child/CN",
                async: true
            }).done(function (data) {
                var i = 0, html = ["<option value='0' selected='selected'>选择省</option>"];
                while (data[i]) {
                    html.push("<option value=" + data[i].id + ">" + data[i].name + "</option>");
                    i++;
                }
                $("#province").html(html.join(''));
            });

        },
        bindEvent: function () {

            //回退
            $("#back").on("tap", function () {
                back();
            });

            //清除
            $("#clear").on("tap", function () {
                $("input").val("");
                $("select option:first").prop("selected", 'selected');
            });

            //选择省
            $("#province,#city").on("change", function () {
                var value = $(this).val(), id = $(this).attr("id");
                $.ajax({
                    type: "GET",
                    url: CONFIG_MANAGER.RequstUrl + "v1/basic/region/child/" + value,
                    async: true
                }).done(function (data) {
                    var i = 0, html = [];
                    while (data[i]) {
                        html.push("<option value=" + data[i].id + ">" + data[i].name + "</option>");
                        i++;
                    }
                    if ("province" == id) {
                        html.unshift("<option value='0' selected='selected'>选择市</option>");
                        $("#city").html(html.join(''));
                    } else {
                        html.unshift("<option value='0' selected='selected'>选择区</option>");
                        $("#country").html(html.join(''));
                    }
                });

            });

            //保存地址
            $("#saveAddress").on("tap", function () {
                if ("" == $("#consignee").val()) {
                    $.alert("请填写收货人姓名");
                    return false;
                }
                if (!isChineseName($("#consignee").val())) {
                    $.alert("请填写正确的收货人姓名");
                    return false;
                }

                if ("" == $("#mobile").val()) {
                    $.alert("请填写手机号");
                    return false;
                }
                if (!isPhone($("#mobile").val())) {
                    $.alert("请填写正确的手机号");
                    return false;
                }

                if ($("#province").get(0).selectedIndex == 0) {
                    $.alert("请选择省");
                    return false;
                }
                if ($("#city").get(0).selectedIndex == 0) {
                    $.alert("请选择省");
                    return false;
                }
                if ($("#country").get(0).selectedIndex == 0) {
                    $.alert("请选择省");
                    return false;
                }

                if ("" == $("#address").val()) {
                    $.alert("请填写收货地址");
                    return false;
                }

                var requestData = {
                        "region_no": $("#country").val(),
                        "address": $("#address").val(),
                        "consignee": $("#consignee").val(),
                        "mobile": $("#mobile").val(),
                        "type": 1,
                        "default_flag": $("#default_flag").prop("checked") ? 1 : 0,
                    },
                    actionUrl = CONFIG_MANAGER.RequstUrl;


                if (addressId) {
                    actionUrl += "v1/member/address/edit/" + user.id + "/" + addressId;
                } else {
                    actionUrl += "v1/member/address/add/" + user.id;
                }

                $.ajax({
                    type: ( addressId > 0 ? "PUT" : "POST"),
                    url: actionUrl,
                    async: true,
                    contentType: 'application/json',
                    data: JSON.stringify(requestData),
                }).done(function (data) {
                    var alertMsg = ( addressId > 0 ) ? "编辑成功" : "添加成功";
                    $.alert(alertMsg, function () { 
                      redirect("address.html");
                      return false; 
                    })
                }).fail(function (data) {
                    var infor = JSON.parse(data);
                    $.toast(infor.Message);
                });


            });
        }
    };

    thisPage = new addAddressPage();

});
